import { onMounted, onUnmounted, ref } from 'vue'

export const useShortcuts = (emit, isShowSearchContent) => {
  // 获取操作系统信息以显示正确的快捷键
  const isMac = /macintosh|mac os x/i.test(navigator.userAgent)
  const shortcut_new = ref(isMac ? '⌘ ⇧ O' : 'Ctrl+Shift+O')
  const shortcut_search = ref(isMac ? '⌘ K' : 'Ctrl+K')
  // 添加快捷键处理函数
  const handleKeyPress = (event) => {
    // Mac 上用 Command，其他用 Ctrl
    const isCmd = isMac ? event.metaKey : event.ctrlKey

    // 检查是否按下 Cmd/Ctrl+Shift+O
    if (isCmd && event.shiftKey && event.key.toLowerCase() === 'o') {
      event.preventDefault()
      emit('add-conversation')
    }

    // 检查是否按下 Cmd/Ctrl+K
    if (isCmd && event.key.toLowerCase() === 'k') {
      event.preventDefault()
      isShowSearchContent.value = true
    }
  }

  // 组件挂载时添加事件监听
  onMounted(() => {
    window.addEventListener('keydown', handleKeyPress)
  })

  // 组件卸载时移除事件监听
  onUnmounted(() => {
    window.removeEventListener('keydown', handleKeyPress)
  })

  return {
    shortcut_new,
    shortcut_search,
  }
}
